package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.V10BindService;
import com.yixun.qfbao.apidto.V10EmployeeInfoDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.OtherSysUser;
import com.yixun.qfbao.model.dto.OtherSysUserDto;
import com.yixun.qfbao.service.OtherSysUserService;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: channel-parent
 *
 * @description: 推送V10绑定员工信息
 *
 * @author: Seven.wang
 *
 * @create: 2019-12-02 10:56
 **/

@Service("pushEmployeeInfoHander")
public class PushEmployeeInfoHander extends CommonAbstractHander {

    @Autowired
    private V10BindService v10BindService;

    @Autowired
    private OtherSysUserService otherSysUserService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<String> res = new BaseResponseDTO<String>();
        OtherSysUserDto entity = new OtherSysUserDto();
        entity.setOrders("t.create_time desc");
        List<OtherSysUser> otherSysUsers = otherSysUserService.selectListByDto(entity);
        List<V10EmployeeInfoDto> employeeInfoDtos = otherSysUsers.stream().map(item -> {
            V10EmployeeInfoDto dto = new V10EmployeeInfoDto();
            dto.setCompanyUuid(item.getOtherCompanyId());
            dto.setEmployeeUuid(item.getOtherUserId());
            return dto;
        }).collect(Collectors.toList());
        Map<String, List<V10EmployeeInfoDto>> collect = employeeInfoDtos.stream()
                .collect(Collectors.groupingBy(t -> t.getCompanyUuid()));
        if(MapUtils.isNotEmpty(collect)){
            v10BindService.asyncV10PushEmployeeInfo(collect);
        }
        res.setResult(ResultEnum.SUCCESS.getNo());
        res.setErrorMsg("操作成功");
        return res;
    }
}
